import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error,r2_score

data = pd.read_csv('D:\\learn\\Artificial_Intelligence-master\\artificial_intelligence\\Chapter2\\usa_housing_price.csv')
fig = plt.figure(figsize=(10,10))
fig1 = plt.subplot(1,5,1)
plt.scatter(data.loc[:, 'Avg. Area Income'], data.loc[:, 'Price'])
plt.title('Avg. Area Income')

fig2 = plt.subplot(1,5,2)
plt.scatter(data.loc[:, 'Avg. Area House Age'], data.loc[:, 'Price'])
plt.title('Avg. Area House Age')

fig3 = plt.subplot(1,5,3)
plt.scatter(data.loc[:, 'Avg. Area Number of Rooms'], data.loc[:, 'Price'])
plt.title('Avg. Area Number of Rooms')

fig4 = plt.subplot(1,5,4)
plt.scatter(data.loc[:, 'Area Population'], data.loc[:, 'Price'])
plt.title('Area Population')

fig5 = plt.subplot(1,5,5)
plt.scatter(data.loc[:, 'size'], data.loc[:, 'Price'])
plt.title('size')

# #单因子模型
# X = data.loc[:,'size']
Y = data.loc[:,'Price']
# X = np.array(X).reshape(-1,1)
# LR1 = LinearRegression()
#
# #训练模型
# LR1.fit(X,Y)
#
# #预测
# y_predict_1 = LR1.predict(X)
#
# #评估模型
# mean_squared_error = mean_squared_error(Y,y_predict_1)
# r2_score = r2_score(Y,y_predict_1)
# print('mean_squared_error:',mean_squared_error,'r2_score:',r2_score)
#
# fig5 = plt.figure(figsize=(8,5))
# plt.scatter(X,Y)
# plt.plot(X,y_predict_1,color='red')

#多因子模型
X_multi = data.drop(['Price'],axis=1)

LR_multi = LinearRegression()
LR_multi.fit(X_multi,Y)

y_predict_multi = LR_multi.predict(X_multi)

mean_squared_error_multi = mean_squared_error(Y,y_predict_multi)
r2_score_multi = r2_score(Y,y_predict_multi)

fig6 = plt.figure(figsize=(8,5))
plt.scatter(Y,y_predict_multi)

X_test = [65000,5,5,30000,200]
X_test = np.array(X_test).reshape(1,-1)

y_test_predict = LR_multi.predict(X_test)
print('y_test:',y_test_predict)